<template>
  <div>
<!--    头部-->
    <div style="display: flex;height: 60px;line-height: 60px;border-bottom: 1px solid #ccc">
      <img src="../../assets/logo.jpg">
      <div style="width: 400px">欢迎来到线上点餐系统!</div>

<!--      导航菜单-->
      <div style="flex: 1">
        <el-menu
            :default-active="path"
            class="el-menu-demo"
            mode="horizontal"
            router
        >
          <el-menu-item index="/front/home">首页</el-menu-item>
          <el-menu-item index="/front/foods">全部菜单</el-menu-item>
          <el-menu-item index="/front/cart">购物清单</el-menu-item>
          <el-menu-item index="/front/order">我的订单</el-menu-item>
          <el-menu-item index="/front/vip">订购会员</el-menu-item>
        </el-menu>
      </div>

<!--      头像区域-->
      <div style="width: 150px;margin-top: 1%">

        <div v-if="user.name">
          <el-dropdown>
            <img :src="user.avatarUrl" alt=""
                 style="width: 30px; border-radius: 50%; position: relative; top: 10px; right: 5px">
            <span class="el-dropdown-link">
              {{user.name}}
              <el-icon class="el-icon--right">
                <!-- 3.使用 -->
                <arrow-down />
              </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="MyInformation">个人信息</el-dropdown-item>
                <el-dropdown-item @click="Exit">退出系统</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        <div v-else>
          <div style="display: flex">
            <el-button @click="$router.push('/front/login')">登录</el-button>
            <el-button @click="$router.push('/front/register')">注册</el-button>
          </div>
        </div>

      </div>


    </div>

<!--路由-->
    <div style="width: 1000px;margin: 0 auto">
      <router-view />
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import {ArrowDown} from "@element-plus/icons-vue";

export default {
  name: "Front",
  created() {
    this.updateUser();
  },
  components :{
    // 2.注册
    ArrowDown,
  },
  data() {
    return {
      user: {},
      path: this.$route.path,
    }
  },
  methods: {
    updateUser() {
      //更新一下最新数据
      let userJson = sessionStorage.getItem("user") || "{}"
      if(userJson){
        let user = JSON.parse(userJson)
        request.get("order/user/getUserById",{params:{id:user.id}}).then((res) =>{
          this.user=res.data
        })
      }else{
        this.$message.error("当前无法修改用户信息!");
        return
      }
    },
    Exit() {
      this.$router.push("/front/login");
    },
    MyInformation(){
      this.$router.push("/front/person");
    }
  },
}
</script>

<style scoped>

</style>